<script setup>
import {
  getFoodRecipeListService,
  deleteFoodRecipeService
} from '../../api/foodRecipe/foodRecipe'
import { onMounted, ref } from 'vue'
import PageContainer from '@/components/pageContainer.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const foodRecipeList = ref([])
const page = ref(1)
const pageSize = ref(8)
const foodRecipeName = ref('')
const total = ref(0)
const loading = ref(true)
const queryParams = ref({
  page: 1,
  pageSize: 8,
  name: null
})
//获取食谱列表
const getFoodRecipeList = async () => {
  try {
    const res = await getFoodRecipeListService(queryParams.value)
    foodRecipeList.value = res.data.data.records
    total.value = res.data.data.total
    loading.value = false
  } catch (e) {
    foodRecipeList.value = []
    total.value = 0
    loading.value = false
  }
}
//获取选中的行
const selects = ref(null)
const handleSelectionChange = (selection) => {
  console.log('selection', selection.length)
  if (selection.length === 0) {
    selects.value = null
    return
  }
  selects.value = selection
}
//删除食谱 -1批量 0单个
const deleteHandle = (type, id) => {
  console.log('selects', selects.value)
  if (type === -1)
    if (!selects.value) {
      ElMessage({
        message: '请选择要删除的食物',
        type: 'warning'
      })
      return
    }
  ElMessageBox.confirm('你确认删除所选食物吗?', '提示', {
    customClass: 'el-message-box-myself',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    confirmButtonClass: 'ElMessageBox-confirm-button-style',
    cancelButtonClass: 'ElMessageBox-cancel-button-style'
  })
    .then(async () => {
      const ids = []
      if (type === -1) {
        selects.value.map((item) => {
          console.log('id', item.id)
          ids.push(item.id)
          console.log('ids', ids)
        })
      } else ids.push(id)

      try {
        const res = await deleteFoodRecipeService(ids.join(','))
        console.log('res', res)

        if (res.data.code === 1) {
          ElMessage({
            type: 'success',
            message: '删除成功'
          })
        } else {
          ElMessage.error(res.data.msg || '操作失败')
        }

        await getFoodRecipeList()
      } catch (e) {
        console.log('请求出错了：' + e)
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除'
      })
    })
}

const handleQuery = () => {
  queryParams.value.page = 1
  getFoodRecipeList()
}
const resetForm = () => {
  queryParams.value.name = null
  handleQuery()
}
//在页面初次渲染执行
onMounted(() => {
  getFoodRecipeList()
})
</script>

<template>
  <div class="floating-box">
    <div>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="68px"
        class="no-wrap"
        @keydown.enter.prevent
      >
        <el-form-item label="食谱名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入食谱名称"
            prefix-icon="Search"
            style="width: 320px"
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item>
          <div class="no-wrap">
            <el-button
              icon="Search"
              plain
              round
              color="#95d475"
              @click="handleQuery()"
              >搜索</el-button
            >
            <el-button
              icon="Refresh"
              plain
              round
              color="#95d475"
              @click="resetForm()"
              >重置</el-button
            >
            <el-button
              icon="Delete"
              plain
              round
              color="#95d475"
              @click="deleteHandle(-1, null)"
              >批量删除</el-button
            >
          </div>
        </el-form-item>
      </el-form>
      <el-divider />
    </div>
    <el-table
      :data="foodRecipeList"
      width="100%"
      :stripe="true"
      size="large"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45" />
      <el-table-column
        label="名称"
        prop="recipeName"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="创建人ID"
        prop="createUser"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="最后操作时间"
        prop="updateTime"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template #default="{ row }">
          <el-button
            type="info"
            plain
            round
            icon="Delete"
            @click="deleteHandle(0, row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        v-show="total > 0"
        :total="total"
        @size-change="getFoodRecipeList"
        @current-change="getFoodRecipeList"
      />
    </div>
  </div>
</template>

<style scoped>
/deep/ .el-input__wrapper {
  border-radius: 50px;
  /*background-color: red;*/
}
</style>
